#ifndef CHARPTER3_2_H
#define CHARPTER3_2_H

#include <iostream>
#include <string>
#include <mutex>
#include <thread>

using namespace std;

class some_data{
public:
    some_data()
    {

    }

public:
    int a;
    std::string b;

public:
    void do_something();
};

void some_data::do_something()
{
    std::cout << "do something: "
              << a
              << b
              << std::endl;
}

class data_wrapper {
private:
    some_data data;
    std::mutex m;

public:
    template<typename Function>
    void process_data(Function func)
    {
        std::lock_guard<std::mutex> l(m);
        func(data);
    }
};

some_data* unprotected;

void malicious_function(some_data& protected_data)
{
    unprotected = &protected_data;
}


data_wrapper x;

void threadFun()
{
    x.process_data(malicious_function);
    unprotected->do_something();
}

void foo()
{
    std::thread tr1(threadFun);
    std::thread tr2(threadFun);
    std::thread tr3(threadFun);

    tr1.join();
    tr2.join();
    tr3.join();
}

#endif CHARPTER3_2_H


